import {
    BASE_URL
} from '../../../http/config';
// index/pages/mentalAnswer/mentalAnswer.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum:'1',
        pageSize:'10',
        time: true,
        endTime: false,
        count: false,
        test: [],      // 原始数据容器
        countss: [],   // 第二个数据容器
        combinedList: [], // 新增合并后的数据容器
        answerList:[],//回答列表
        publishCount: 0, // 发布的数量
        replyCount: 0    // 回复的数量
    },
    onLoad: function () {
        this.loadData();
    },
     // 新增：封装数据加载方法
  loadData() {
    const getTestList = new Promise((resolve) => {
      wx.request({
        url: `${BASE_URL}/question/selectDuo`,
        success: (res) => {
          this.setData({ test: res.data.data.list });
          resolve();
        },
        fail: () => resolve()
      });
    });

    const getCountssList = new Promise((resolve) => {
      wx.request({
        url: `${BASE_URL}/question/selectCounts`,
        method: "GET",
        success: (res) => {
          // 修复：使用setData更新
          this.setData({ countss: res.data.data });
          resolve();
        },
        fail: () => resolve()
      });
    });

    Promise.all([getTestList, getCountssList]).then(() => {
      this.mergeData();
    });
  },
   // 新增：合并数据方法
   mergeData() {
    const { test, countss } = this.data;
    const combined = test.map(item => {
      const matched = countss.find(c => c.id === item.id);
      return { 
        ...item, 
        answerCount: matched?.answerCount || 0
      };
    });
    // 按回复数降序排序
    combined.sort((a, b) => b.answerCount - a.answerCount);
    this.setData({ combinedList: combined });
    console.log(this.data.combinedList);
  },
    // 事件处理
    onTime() {
        this.setData({
            time: true,
            endTime: false,
            count: false
        })
    },
    //获取页面数据
    onEndTime() {
        this.setData({
            time: false,
            endTime: true,
            count: false
        });
        this.loadLastReplyData(); // 新增：切换到最后回复时加载数据
    },
    // 新增：加载最后回复数据
    loadLastReplyData(cb) {
        wx.request({
            url: `${BASE_URL}/answer/selectDuo`,
            method: 'GET',
            data: {
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize
            },
            success: (res) => {
                if (res.data && res.data.data && res.data.data.list) {
                    this.setData({ answerList: res.data.data.list }, () => {
                        if (cb) cb();
                    });
                } else {
                    this.setData({ answerList: [] }, () => {
                        if (cb) cb();
                    });
                }
            },
            fail: () => {
                this.setData({ answerList: [] }, () => {
                    if (cb) cb();
                });
            }
        });
    },
    onCount() {
        this.setData({
            time: false,
            endTime: false,
            count: true
        });
        // 如果answerList还没加载，先加载
        if (!this.data.answerList || this.data.answerList.length === 0) {
            this.loadLastReplyData(() => {
                this.setData({
                    publishCount: this.data.test.length,
                    replyCount: this.data.answerList.length
                });
            });
        } else {
            this.setData({
                publishCount: this.data.test.length,
                replyCount: this.data.answerList.length
            });
        }
    },
    toQa() {
        wx.navigateTo({
            url: '/index/pages/kefu/kefu',
        })
    },
    onReply() {
        wx.navigateTo({
            url: '/index/pages/reply/reply',
        })
    },
    onDesc(e) {
        console.log(e)
        const id=e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/index/pages/particulars/particulars?id='+id,
            
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})